package com.afinal.intelligencetrash.Model.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.afinal.intelligencetrash.Model.bean.User;
import com.afinal.intelligencetrash.Model.interfaces.LoginModel;
import com.afinal.intelligencetrash.Model.interfaces.RegisterModel;
import com.afinal.intelligencetrash.Model.interfaces.RequestHolder;
import com.afinal.intelligencetrash.global.CustomApplication;
import com.afinal.intelligencetrash.utils.DebugLog;

import javax.inject.Singleton;

/**
 * Created by cyy on 17-10-8.
 */

@Singleton
public class LocalUserDB implements LoginModel, RegisterModel {
    private UserDBSQLHelper dbHelper;

    @Override
    public void login(String username, String pwd, RequestHolder<User> requestHolder) {
        if(dbHelper == null ){
            dbHelper = new UserDBSQLHelper(CustomApplication.getCustomApplicationContext(), UserDBSQLHelper.DB_NAME, null, UserDBSQLHelper.VERSION);
        }
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor result = db.query(UserDBSQLHelper.TABLE_NAME,
                    new String[]{UserDBSQLHelper.Column.DATA},
                    UserDBSQLHelper.Column.USER_NAME+"=? AND "+UserDBSQLHelper.Column.PWD+"=?",
                    new String[]{username, pwd},null, null, null
                );
        if (result.moveToNext()){
            String r = result.getString(result.getColumnIndex(UserDBSQLHelper.Column.DATA));
            requestHolder.onRequestSuccess(r);
        }else {
            Cursor result2 = db.query(UserDBSQLHelper.TABLE_NAME,
                    new String[]{UserDBSQLHelper.Column.DATA},
                    UserDBSQLHelper.Column.USER_NAME+"=?",
                    new String[]{username},null, null, null
            );
            if(!result2.moveToNext()){
                requestHolder.onRequestFailure(403, "该用户不存在");
            }else {
                requestHolder.onRequestFailure(404, "用户名或者密码错误");
            }
        }
    }

    @Override
    public void register(String username, String pwd, RequestHolder<User> holder) {
        if(dbHelper == null){
            dbHelper = new UserDBSQLHelper(CustomApplication.getCustomApplicationContext(), UserDBSQLHelper.DB_NAME, null, UserDBSQLHelper.VERSION);
        }
        User user = new User();
        user.setPwd(pwd);
        user.setUsername(username);
        String json = user.toJson();
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(UserDBSQLHelper.Column.USER_NAME, username);
        values.put(UserDBSQLHelper.Column.PWD, pwd);
        values.put(UserDBSQLHelper.Column.DATA, json);

        long r = db.insert(UserDBSQLHelper.TABLE_NAME, null, values);
        if(r != -1){
            holder.onRequestSuccess(json);
        }else{
            Cursor cursor = db.query(UserDBSQLHelper.TABLE_NAME, null, UserDBSQLHelper.Column.USER_NAME+"=?", new String[]{username}, null, null, null);
            if(cursor.moveToNext()){
                holder.onRequestFailure(405, "该用户名已存在");
            }else {
                holder.onRequestFailure(406, "注册失败");
            }
        }
    }

    public static LocalUserDB instance = null;
    public static LocalUserDB getInstance(){
        if (instance == null){
            synchronized (LocalUserDB.class){
                if (instance == null){
                    instance = new LocalUserDB();
                }
            }
        }
        return instance;
    }

}
